/**
 * 双端队列
 */
export default class Deque<T>{
    private _count:number = 0;
    private _lowCount:number = 0;
    private _items:{[key:number]:T} = {};

    public addFront(item:T){
        this._items[this._lowCount] = item;
        this._lowCount--;
    }
    public addBack(item:T){
        this._items[this._count] = item;
        this._count++;
    }
    public removeFront():T{
        let result = this._items[this._lowCount];
        delete this._items[this._lowCount];
        this._lowCount++;
        return result;
    }
    public removeBack():T{
        let result = this._items[this._count];
        delete this._items[this._count];
        this._count--;
        return result;
    }
    public peekFront():T{
        return this._items[this._lowCount];
    }
    public peekBack():T{
        return this._items[this._count];
    }
    public isEmpty():boolean{
        return this._count - this._lowCount == 0;
    }
    public clear(){
        this._count = 0;
        this._lowCount = 0;
        this._items = {};   
    }
    public size(){
        return this._count - this._lowCount;
    }
    public toString(){
        if(this.isEmpty()){
            return "";
        }
        let str = "";
        for(let i = this._lowCount;i<this._count;i++){
            str+=`${i},${this._items[i]} `
        }
        return str;
    }
}